using FireStarterModeller.BO;
using Habanero.Base.Exceptions;

namespace FireStarterModeller.Logic.Commands
{
    public class RenameAssemblyCommand : ICommand
    {
        private readonly DMAssembly _assembly;
        private readonly string _newAssemblyName;
        private string _originalName;

        public RenameAssemblyCommand(DMAssembly assembly, string newAssemblyName)
        {
            _assembly = assembly;
            _newAssemblyName = newAssemblyName;
        }

        public void Apply() {
            _originalName = _assembly.Name;
            _assembly.Name = _newAssemblyName;
            try
            {
                _assembly.Save();
            } catch (HabaneroApplicationException)
            {
                _assembly.Props["Name"].RestorePropValue();
                throw;
            }
        }
        public void Revert() {
            _assembly.Name = _originalName;
            try
            {
                _assembly.Save();
            }
            catch (HabaneroApplicationException)
            {
                _assembly.Props["Name"].RestorePropValue();
                throw;
            }            
        }
    }
}